<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $yinhang=$_REQUEST['yinhang'];
        $zhihang=$_REQUEST['zhihang'];
        $kahao=$_REQUEST['kahao'];
        $xingming=$_REQUEST['xingming'];
		$sql="select * from `".$oto."_users` where `userId`='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_agentset` where `status`='1' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$agent_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$time=time();
		$ktx_yj=$user_info[0]['agentBalance'];
		$sqz_yj=$user_info[0]['agentWaitPrice'];
		$min=$agent_info[0]['minApplyPrice'];
		$max=$agent_info[0]['maxApplyPrice'];
		$loginName=$user_info[0]['loginName'];
		$tel=$user_info[0]['userPhone'];
		$tx_yj=0;
		if($ktx_yj==0)
		{
			$data[0]['pan']=0;
			echo json_encode($data);
			exit();
		}
		else
		{
			if($min==0) ;
			else
			{
				if($ktx_yj<$min)
				{
					$data[0]['pan']=0;
					echo json_encode($data);
					exit();
				}
			}
			if($max==0) $tx_yj=$ktx_yj;
			else
			{
				if($max<$ktx_yj) $tx_yj=$max;
				else $tx_yj=$ktx_yj;
			}
			$sql="select * from `".$oto."_agent_apply` where `userId`='{$user_id}' and `status`='0'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$isAgent[]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(isset($isAgent))
			{
				$data[0]['pan']=-1;
				$data[0]['msg']="已有申请待处理中";
				echo json_encode($data);
				exit();
			}
			$sql="INSERT INTO ".$oto."_agent_apply (`userId`,`loginName`,`applyType`,`userType`,`tel`,`applyPrice`,`bankUserName`,`bankName`,`bankAccess`,`status`,`bankNum`,`time`) VALUES ('$user_id','$loginName','0','0','$tel','$tx_yj','$xingming','$yinhang','$zhihang','0','$kahao','$time')";
            $result=$db->query($sql);
            $balance=$ktx_yj-$tx_yj;
            $waitprice=$sqz_yj+$tx_yj;
            $sql="update ".$oto."_users set agentBalance='{$balance}',agentWaitPrice='{$waitprice}' where userId='{$user_id}'";
			$result=$db->query($sql);
			$data[0]['pan']=1;
		}
		echo json_encode($data);
?>